package org.mayanjun.code.easyweb.springmvc.config;

import org.slf4j.Logger;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * PropertiesResourcesLogger used to print resources log
 *
 * @author mayanjun(1/31/16)
 */
class PropertiesResourcesLogger {

    private boolean logPrinted;
    private Logger logger;

    public PropertiesResourcesLogger(Logger logger) {
        this.logger = logger;
        this.logPrinted = false;
    }

    public void log(String propsName, Resource res[]) {
        synchronized (this) {
            if(!logPrinted) {
                StringBuilder sb = new StringBuilder();
                if(res != null && res.length > 0) {
                    sb.append("Using properties with name: " + propsName + "[");
                    for(Resource r : res) {
                        if(r != null) {
                            try {
                                sb.append(r.getURL() + ",");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    sb.append("]");
                }
                logger.info(sb.toString());
                logPrinted = true;
            }
        }
    }
}
